home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / unix / src / namefind.c < prev    next >
C/C++ Source or Header  |  1993-11-01  |  2KB  |  146 lines

  1.  
  2. #include "cb.h"
  3.  
  4. int cmp_name();
  5.  
  6. namefind(cp,name,pagit,printcall)
  7. FILE *cp;
  8. char *name;
  9. int pagit;
  10. int *(printcall)();
  11. {
  12.     char *p;
  13.     char *d;
  14.     caddr_t mi;
  15.     char *list[8];
  16.     char **l;
  17.     char fmtname[128];
  18.     char buf[512];
  19.     char c;
  20.     int idsize;
  21.     FILE *fp;
  22.     int n;
  23.     int ns;
  24.     struct nind key;
  25.     struct nind *idx;
  26.     struct nind *found;
  27.     struct nind *end;
  28.     struct stat st;
  29.     int IS = sizeof(struct nind);
  30.  
  31.     if ((fp=fopen("nlist.ndx","r")) == NULL)
  32.     {
  33.         printf("Error opening nlist.ndx\n");
  34.         exit(1);
  35.     }
  36.     fstat(fileno(fp),&st);
  37.     if ((mi = (caddr_t)mmap(NULL,st.st_size,PROT_READ,MAP_SHARED,
  38.         fileno(fp),NULL)) == (caddr_t)NULL)
  39.     {
  40.         fprintf(stderr,"Error - mmap index file\n");
  41.         exit(1);
  42.     }
  43.     fclose(fp);
  44.  
  45.     idsize = st.st_size/IS;
  46.     idx = (struct nind *)mi;
  47.  
  48.     p = name;
  49.     while (*p)
  50.     {
  51.         switch (*p)
  52.         {
  53.         case ',':
  54.         case '.':
  55.         case '\'':
  56.             *p = ' ';
  57.             break;
  58.         default:
  59.             break;
  60.         }
  61.         p++;
  62.     }
  63.     if ((n = tokenize(name,list,8)) == NULL)
  64.         return(0);
  65.  
  66.     memset(key.name,0,sizeof(key.name));
  67.     memset(fmtname,0,sizeof(fmtname));
  68.     l = list;
  69.     while (*l)
  70.     {
  71.         strcat(fmtname,*l++);
  72.         strcat(fmtname," ");
  73.     }
  74.     p = strrchr(fmtname,' ');
  75.     *p = '\0';
  76.  
  77.     strncpy(key.name,fmtname,sizeof(key.name)-1);
  78.     p = key.name;
  79.     while (*p)
  80.         *p++ = toupper(*p);
  81.  
  82.     found = (struct nind *)bsearch((char *)(&key),
  83.         (char *)idx, idsize, sizeof(struct nind), cmp_name);
  84.  
  85.     if (!found)
  86.     {
  87.         munmap(mi,st.st_size);
  88.         return(0);
  89.     }
  90.  
  91.     end = idx + IS;
  92.     ns = strlen(key.name);
  93.     while ( found && !strncmp(key.name,found->name,ns))
  94.         found--;
  95.     found++;
  96.  
  97.     n = 0;
  98.     while ( found && (found != end) && !strncmp(key.name,found->name,ns))
  99.     {
  100.         fseek(cp,found->pos,SEEK_SET);
  101.         fread(buf,sizeof(buf),1,cp);
  102.         p = buf;
  103.         while (*p != '\n')
  104.             *p++;
  105.         *p = '\0';
  106.  
  107.         pcall(buf,printcall);
  108.         found++;
  109.         n++;
  110.         if (pagit && (n%5 == 0))
  111.             if ((c=n_any_key(1,printcall)) == 'Q')
  112.                 break;
  113.     }
  114.     munmap(mi,st.st_size);
  115.     return(n);
  116. }
  117.  
  118. cmp_name(i1,i2)
  119. struct nind *i1, *i2;
  120. {
  121.     int r;
  122.     int n = strlen(i1->name);
  123.  
  124.     r = (strncmp(i1->name,i2->name,n));
  125.     return (r);
  126. }
  127.  
  128.  
  129. n_any_key(q,printcall)
  130. int q;
  131. int *(printcall)();
  132. {
  133.     char c;
  134.  
  135.     if (q)
  136.         printcall("<Press any key to continue or Q)uit ==> ");
  137.     else
  138.         printcall("<Press any key to continue> ");
  139.  
  140.     c = toupper((char)getchar());
  141.     printcall("\n\r");
  142.     return(c);
  143. }
  144.  
  145.  
  146.